var http = require("http");
var fs = require("fs");

function serveStaticFile(res, path, contentType, responseCode) {
  if (!responseCode) responseCode = 200;
  fs.readFile(__dirname + path, function (err, data) {
    if (err) {
      res.writeHead(500, { "Content-Type": "text/plain" });
      res.end("500-InternaError");
    } else {
      res.writeHead(responseCode, { "Content-Type": contentType });
      res.end(data);
    }
  });
}

http.createServer((req,res) => {
    var path = req.url.replace(/\/?(?:\?.*)?$/,'')
    switch(path){
        case '':
            serveStaticFile(res,'/public/index.html','text/html')
            break
        case '/json':
            serveStaticFile(res,'/public/demo.json','text/json')
            break
        default:
            serveStaticFile(res,'/public/404.html','text/html',404)
            break
    }
}).listen(3000)